<?php

namespace App\Http\Controllers\Admin;

use App\Models\ActivityGoods;
use App\Models\ActivityCat;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use App\Commands\OperationLogCommand;

class ActivityGoodsController extends Controller
{

    public function activity_goods_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    public function index(Request $request)
    {
        $this->activity_goods_gate();

        $param = array();
        $activity_id = $request->get("activity_id");
        $activity_cat_id = $request->get("id");

        if($activity_id){
            $activityCat = ActivityCat::where('activity_id',$activity_id)->paginate(10);
            foreach($activityCat as $li){
                $activityCatArr[$li->id]=$li->cat_name;

            }
        }

        if($activity_cat_id){

            $activityGoods = ActivityGoods::orderBy('sort_order','desc')->orderBy('id','desc')->where('activity_cat_id',$activity_cat_id)->paginate(10);


        }


        return view('admin.activity_goods.index',compact('activityGoods','activity_cat_id','activity_id','activityCatArr'));
    }


      public function destroy(Request $request , $id)
    {
        $this->activity_goods_gate();

        //dd($request->get("url"));
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $activityGoods = ActivityGoods::find($id);

        $activityGoods->delete();
        $this->dispatch(
            new OperationLogCommand('删除活动商品','ActivityGoods','id:'.$id)
        );
        return $return;
        //return Redirect($request->get("url"));
    }



     /**
     * 排序
     * @route admin.goods_attribute.sort
     * @return Response
     */
    public function sort(Request $request)
    {

        $this->activity_goods_gate();

        $url = $request->get('url');
        //dd($request->all());
        $activityGoods = $request->get('sort_order');

        if (is_array($activityGoods)&&count($activityGoods)>0)
        {
            foreach($activityGoods as $k=>$v)
            {

                $sort_order = ActivityGoods::where('sort_order',$v)->get();

                ActivityGoods::find($k)->update(['sort_order' => $v]);

            }
        }

        return redirect($url);

    }

}
